package images;

import data.DataGenerator;
import data.ExtramobiExcel;
import data.ItemData;
import data.obj.Repair;
import jxl.read.biff.BiffException;
import mat.xls.ExcelParser;
import obj.Item;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.Map;

public class PicsClone {
    public static void main(String[] args) throws IOException, BiffException {
//        String outPath = "images/extramobi/test/";
//        List<Item> items = Item.parseItemsXlsDir("data/extramobi/plenki/");
//        List<Item> items = Item.parseItemsXlsDir("data/extramobi/accum-prowin/");
//        cloneImages("images/extramobi/plenki/", "plenka", "", "images/extramobi/gen/plenka%s.jpg", items);
//        cloneImages("images/extramobi/dmitry/accum-prowin/", "accum prowin", "2", "images/extramobi/dmitry/accum-prowin%s.jpg", items);
//        String outPath = "images/extramobi/accum/";
//        String postfix = "2";
//        File imageToClone = new File("images/extramobi/gen/accum-htc"+postfix+".jpg");
//        File imageToClone = new File("images/extramobi/gen/accum-samsung"+postfix+".jpg");
//        File imageToClone = new File("images/extramobi/gen/accum-samsung2.jpg");
//        File imageToClone = new File("images/extramobi/gen/accum-craftmann.jpg");
//        String prefix = "accum";
//        List<Item> items = Item.parseItemsXlsFile("data/extramobi/accum/accum.htc.xls");
//        List<Item> items = Item.parseItemsXlsFile("data/extramobi/accum/accum.samsung.xls");
//        List<Item> items = Item.parseItemsXlsFile("data/extramobi/accum-craft/accum-craft.craftmann.xls");
//        cloneImage(outPath, imageToClone, items, prefix, postfix);

        toLowerCaseFiles("G:\\projects\\my-im-tools\\images\\extramobi\\charger\\");

//        copyFiles("images/products_pictures/", "images/accum/", true, "accum ");

//        removePrefixFiles("images/charger", "accum ");
//        addPrefixFiles("images/charger", "charger ");
//        removePrefixFiles("images/body", "accum ");
//        addPrefixFiles("images/body", "body ");
//        removePrefixFiles("images/gen", "accum ");
//        addPrefixFiles("images/headset", "headset ");
//        toOldFiles(path);
//        cloneImage(path, imageToClone, 52);

        renameFiles("images/cellmart/accum/");
    }

    public static void renameFiles(String picsPath) throws IOException {
        for (String picFile : new File(picsPath).list()) {
            String newPicFile = picFile.replace("accum", "battery");
            new File(picsPath + picFile).renameTo(new File(picsPath + newPicFile));
            System.out.println(picsPath + newPicFile);
        }
    }

    public static void cloneImages(String outPath, String prefix, String postfix, String imageToClone, List<Item> items) throws IOException {
        File imageToCloneFile = new File(String.format(imageToClone, postfix));
        cloneImage(outPath, imageToCloneFile, items, prefix, postfix);
    }

    private static void removePrefixFiles(String path, String prefix) {
        File folder = new File(path);
        for (File file : folder.listFiles()) {
            if (file.getName().startsWith(prefix)) {
                String fileName = file.getName().substring(prefix.length(), file.getName().length());
                file.renameTo(new File(path + "/" + fileName));
            }
        }
    }

    public static void copyFiles(String pathFrom, String pathTo, boolean onlyOrig, String prefixToAdd) throws IOException {
        File fromFolder = new File(pathFrom);
        for (File file : fromFolder.listFiles()) {
            String fileName = file.getName();
            if (onlyOrig) {
                if (fileName.contains("large") || fileName.contains("small") || fileName.contains("medium") || fileName.contains(".svn")) {
                    continue;
                }
            }
            FileChannel source = new FileInputStream(file).getChannel();
            FileChannel dest = new FileOutputStream(pathTo + prefixToAdd + fileName).getChannel();
            dest.transferFrom(source, 0, source.size());
            source.close();
            dest.close();
        }
    }

    private static void addPrefixFiles(String path, String prefix) {
        File folder = new File(path);
        for (File file : folder.listFiles()) {
            file.renameTo(new File(path + "/" + prefix + file.getName()));
        }
    }

    private static void toLowerCaseFiles(String path) {
        File folder = new File(path);
        for (File file : folder.listFiles()) {
            file.renameTo(new File(path + "/" + file.getName().toLowerCase()));
        }
    }

    private static void toOldFiles(String path) {
        File folder = new File(path);
        for (File file : folder.listFiles()) {
            if (file.getName().contains("-old")) {
                continue;
            } else {
                String[] fields = file.getName().split("\\.");
                file.renameTo(new File(path + "/" + fields[0] + "-old." + fields[1]));
            }
        }
    }

//    private static void cloneHeadsets() throws IOException {
//        BufferedReader dataReader = new BufferedReader(new FileReader("data/headsets/headset.nokia.txt"));
//        String line;
//        while ((line = dataReader.readLine()) != null) {
//            String name = line.split(";")[0];
//            BufferedImage image = ImageIO.read(new File("images/headsets/" + name + ".jpg"));
//
//        }
//    }

    private static void cloneImage(String path, File imageToClone, String dataPath, String prefix, boolean printModels) throws BiffException, IOException {
        List<Repair> phones = ExcelParser.parseRepair(dataPath, 100);
        for (Repair phone : phones) {
            BufferedImage image = ImageIO.read(imageToClone);
            if (printModels) {
                for (String model : phone.models) {
                    model = DataGenerator.translateToEng(model).toLowerCase();
                    phone.vendor = DataGenerator.translateToEng(phone.vendor).toLowerCase();
                    ImageIO.write(image, "jpg", new File(path + prefix + phone.vendor + " " + model + ".jpg"));
                }
            } else {
                phone.vendor = DataGenerator.translateToEng(phone.vendor).toLowerCase();
                ImageIO.write(image, "jpg", new File(path + prefix + phone.vendor + ".jpg"));
            }
        }
    }

    private static void cloneImage(String outPath, File imageToClone, List<Item> items, String prefix, String postfix) throws IOException {
        for (Item item : items) {
            String name = item.name;
            if (item.props.get("Производитель") != null && item.props.get("Производитель").equalsIgnoreCase("Craftmann")) {
                name = "Craftmann " + name;
            }
            BufferedImage image = ImageIO.read(imageToClone);
            String fileName = outPath + prefix + " " + name + postfix + ".jpg";
            ImageIO.write(image, "jpg", new File(fileName.toLowerCase()));
        }
    }

    private static void cloneImage(String path, File imageToClone, BufferedReader dataReader) throws IOException {
        String line;
        while ((line = dataReader.readLine()) != null) {
            String name = line.split(";")[0];
            BufferedImage image = ImageIO.read(imageToClone);
            ImageIO.write(image, "jpg", new File(path + name + ".jpg"));
        }
    }

    private static void cloneImage(String path, File imageToClone, int fileCount) throws IOException {
        for (int fileCnt = 1; fileCnt < fileCount; fileCnt++) {
            BufferedImage image = ImageIO.read(imageToClone);
            ImageIO.write(image, "png", new File(path + fileCnt + "-s" + ".png"));
        }
    }
}
